//
// Created by xbm on 23-3-10.
//

#include "dispatcher.h"
#include <future>
#include <cassert>
#include <memory>
#include <utility>
#include "../threadPool/threadPool.h"
#include "handler.h"

using namespace std;

using namespace event;

void Dispatcher::dispatch(const shared_ptr<Event> &e) {
    assert(registered.contains(e->type.num));
    static auto &ins = threadPool::ThreadPool::getInstance();
    auto job = make_shared<packaged_task<void()>>([Func = registered[e->type.num], e] { return Func(e); });
    auto res = ins.commit(job);
    ResultSupervisorHandler::addResult(std::move(res));
}

void Dispatcher::registerHandler(int type, HandlerType handle) {
    registered[type] = std::move(handle);
}
